home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
rjs.lha
/
RJS
/
CmdLine
/
src
/
CmdLine.C
next >
Wrap
C/C++ Source or Header
|
1991-06-14
|
4KB
|
164 lines
#include "CmdLine.h"
#include <stdlib.h>
//#include <string.h>
#include <osfcn.h>
#include <iostream.h>
RJS_CmdLine::RJS_CmdLine(RJS_CmdOpt **opt, const char *p,CmdLineFlags f)
{
prefix=p;
flags=f;
options=opt;
if (abbrev()) setup_abbrevs();
else for (int i=0; options[i] != NULL; i++)
options[i]->abbrev_len = options[i]->keyword().length();
if (dcl()) sep='/'; else sep='-';
}
void RJS_CmdLine::setup_abbrevs()
{
int i,j,l,min_len;
for (i=0; options[i] != NULL; i++) {
options[i]->abbrev_len=1;
for (j=0; options[j] != NULL; j++) if (i==j) continue; else {
min_len= (options[i]->keyword().length() <= options[j]->keyword().length()) ?
options[i]->keyword().length() : options[j]->keyword().length();
for (l=0; l<min_len; l++)
if (options[i]->keyword()[l] != options[j]->keyword()[l]) break;
l++;
if (l > options[i]->abbrev_len) {
if (l < options[i]->keyword().length())
options[i]->abbrev_len=l;
else
options[i]->abbrev_len=options[i]->keyword().length();
}
}
}
}
RJS_CmdLine::ParseStatus RJS_CmdLine::parse(char **argv, RJS_String &xtra)
{
RJS_CmdLineWord clw(argv);
return parse(clw,xtra);
}
RJS_CmdLine::ParseStatus RJS_CmdLine::parse(RJS_String &cmdline, RJS_String &xtra)
{
RJS_CmdLineWord clw(cmdline,dcl());
return parse(clw,xtra);
}
RJS_CmdLine::ParseStatus RJS_CmdLine::parse(RJS_CmdLineWord &clw, RJS_String &xtra)
{
xtra="";
int i;
int no_more_options=0;
for (i=0; options[i] != NULL; i++) options[i]-> reset();
while (clw(opt)) {
if (opt.contains('=')) {
val=opt.after('=');
opt=opt.before('=');
} else val="";
if (opt[0]!=sep || opt=='-' || no_more_options) {
// parameter checking here!
xtra += (opt + ' ');
continue;
} else if ( opt.length()>1 && opt[0]=='-' && opt[1]=='-') {
no_more_options=1;
continue;
}
RJS_String copt = opt(1); // remove separtor ('-'|'/')
for (i=0; options[i] != NULL; i++) {
if (copt.length() > options[i]->keyword().length() ||
copt.length() < options[i]->abbrev_len) continue;
if (no_case()) {
if (options[i]->keyword().substr(0,copt.length())==copt.icase())
break;
}
else if (options[i]->keyword().substr(0,copt.length())==copt) break;
}
if (options[i]==NULL) {
if (messages())
cerr << prefix << ": error unknown option: "<< opt << endl;
if (abort()) exit(1); else return UnknownOption;
}
if (options[i]->is_present() && only_once()) {
if (messages())
cerr << prefix << ": option specified more then once:" <<sep<< opt << endl;
if (abort()) exit(1); else return MoreThenOnce;
}
if (options[i]->expects_value()) { // option has a required valu
if (val=="") {
if (options[i]->value_optional()) {
RJS_String temp;
if(clw.peek(temp) && (temp[0]!=sep || temp=="--"))
clw(val);
else {
options[i]->set();
continue; // get next option
}
} else clw(val);
}
if (!val) {
if (messages())
cerr << prefix << ": missing " << options[i]->value_type()
<< " arguement for " <<sep<< options[i]->keyword() << endl;
if (abort()) exit(1); else return NoValue;
} else {
if (!options[i]->set(val)) {
if (messages())
cerr << prefix << ": invalid arguement for "<<sep <<
options[i]->keyword() << ", expecting "<<
options[i]->value_type() << endl;
if (abort()) exit(1); else return BadValue;
}
}
} else options[i]->set();
} // end of while
for (i=0; options[i] != NULL; i++) {
if (options[i]->is_required() && !options[i]->is_present()) {
if (messages())
cerr << prefix << ": "<<sep << options[i]->keyword() <<
" is required" << endl;
if (abort()) exit(1); else return MissingRequired;
}
if (options[i]->has_default() && !options[i]->is_present()) {
if (!options[i]->set_default()) {
if (messages())
cerr << prefix << ": invalid arguement for "<<sep <<
options[i]->keyword() << ", expecting "<<
options[i]->value_type() << endl;
if (abort()) exit(1); else return BadValue;
}
}
}
return Ok;
}
void RJS_CmdLine::dump()
{
for (int i=0; options[i] != NULL; i++) options[i]->dump();
}